package Course12;

import java.util.ArrayList;

public class Array2 {
    public static void main(String[] args) {
        ArrayList<String>list=new ArrayList<>();
        list.add("Item1");
        list.add("Item2");
        list.add(2,"Item3");
        list.add("Item4");


        int pos=list.indexOf("Item2");
        System.out.println("The index of Item2 is:"+pos);

        boolean check=list.isEmpty();
        System.out.println("Checking if the arraylist is empty:"+check);

        int size=list.size();
        System.out.println("The size of the list is:"+size);

        boolean element=list.contains("Item5");
        System.out.println("Checking if the arrlist contains the object Item5:"+element);

        String item =list.get(0);
        System.out.println("The item is the index 0 is:"+item);

        list.set(1,"NewItem");
        list.set(2,"bmatch");
        System.out.println("The arraylist after the replacement is:"+list);

        list.remove(0);
        System.out.println("The arraylist after the replacement is:"+list);
        list.remove(1);
        System.out.println("The arraylist after the replacement is:"+list);

        list.remove("Item3");
        list.remove("Item4");
        System.out.println("The arraylist after the replacement is:"+list);

        try{
            list.add(5,"Item5");
        }
        catch(Exception e){
            System.out.println(e.getMessage());
        }
    }
}
//将5改成4后程序可以正常运行，看来原因应该是因为arraylist不能跳位置储存对象，原来储存了3个，就不能直接先储存第5个。